package com.kyd.push.mq;

import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;

import com.kyd.push.mq.properties.MQProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * MQConfig
 *
 * @author kejw on 2018/9/27
 */
//@Configuration
//@EnableConfigurationProperties(MQProperties.class)
public class MQAutoConfiguration {

	@Autowired
	private static MQProperties mqProperties;

	/**
	 * MQ 生产者
	 *
	 * @return
	 */
	@Bean(name = "producer", initMethod = "start", destroyMethod = "shutdown")
	@ConditionalOnClass(ProducerBean.class)
	public Producer producer() {
		Properties properties = new Properties();
		// 您在 MQ 控制台创建的 Producer ID
		properties.put(PropertyKeyConst.ProducerId, mqProperties.getAliyun().getProducer().getProducerId());
		// 鉴权用 AccessKey，在阿里云服务器管理控制台创建
		properties.put(PropertyKeyConst.AccessKey, mqProperties.getAliyun().getProducer().getAccessKey());
		// 鉴权用 SecretKey，在阿里云服务器管理控制台创建
		properties.put(PropertyKeyConst.SecretKey, mqProperties.getAliyun().getProducer().getSecretKey());
		//设置发送超时时间，单位毫秒
		properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, mqProperties.getAliyun().getProducer().getSendMsgTimeoutMillis());
		// 设置 TCP 接入域名（此处以公共云的公网接入为例）
		properties.put(PropertyKeyConst.ONSAddr, mqProperties.getAliyun().getProducer().getOnsAddr());

		ProducerBean producer = new ProducerBean();
		producer.setProperties(properties);
		return producer;
	}

}
